#include<bits/stdc++.h>
using namespace std;
int n, q, c;
int col[200005];
int fa[200005];
vector<int>t[200005], g[200005];
vector<int>son[200005];
vector<int> b[200005];
int s[200005];
int f[200005], sz[200005];
vector<pair<int, int> > vec[1<<16];
int ans[200005];
int find(int x){
    return f[x] == x ? f[x] : find(f[x]);
}
void add(int now, int x, vector<pair<int, int> > &p){
    for(auto i : b[x]){
        int u = find(i);
        for(auto to : g[i]){
            if(now >> col[to] & 1){
                int v = find(to);
                p.emplace_back(u, v);
                f[v] = u;
                sz[u] += sz[v];
            }
        }
    }
}
void cx(vector<pair<int, int> > p){
    reverse(p.begin(), p.end());
    for(auto [u,v] : p){
        sz[u] -= sz[v];
        f[v] = v;
    }
}
void dfs(int now){
    for(auto [bh, u] : vec[now]){
        ans[bh] = sz[find(u)];
    }
    for(auto to : son[now]){
        vector<pair<int, int> >awa;
        add(to, __lg(to ^ now), awa);
        dfs(to);
        cx(awa);
    }
}
signed main(){
    freopen("tree.in", "r", stdin);
    freopen("tree.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin >> n >> q >> c;
    for(int i = 1; i <= n; i++){
        cin >> col[i];
        b[col[i]].emplace_back(i);
    }
    for(int i = 2; i <= n; i++){
        cin >> fa[i];
        t[fa[i]].emplace_back(i);
        t[i].emplace_back(fa[i]);
    }
    for(int i = 1; i <= n; i++){
        for(auto to : t[i]){
            if(col[to] == col[i]){
                if(i < to)continue;
            }
            g[i].emplace_back(to);
        }
    }
    for(int i = 1; i <= n; i++){
        s[col[i]] += t[i].size();
    }
    for(int i = 1; i < (1<<c); i++){
        int mn = 1e9, p = 0;
        for(int j = 0; j < c; j++){
            if(i >> j & 1){
                if(mn > s[j]){
                    mn = s[j];
                    p = j;
                }
            }
        }
        son[i^(1<<p)].emplace_back(i);
    }
    for(int i = 1; i <= n; i++)f[i] = i, sz[i] = 1;
    for(int i = 1; i <= q; i++){
        int S, u;
        cin >> S >> u;
        vec[S].emplace_back(i, u);
    }
    dfs(0);
    for(int i = 1; i <= q; i++){
        cout << ans[i] << '\n';
    }
    return 0;
}